package com.gao.gLotusMusic.view;

import com.gao.musicApi.model.Song;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

import java.io.File;
import java.util.List;


public class SongTable extends TableView<Song> {

    public SongTable() {
        initUI();
    }
    private void initUI() {
        TableColumn<Song, String> nameColumn = new TableColumn<>("name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        TableColumn<Song, String> artistColumn = new TableColumn<>("artist");
        artistColumn.setCellValueFactory(new PropertyValueFactory<>("singer"));
        TableColumn<Song, String> lengthColumn = new TableColumn<>("length");
        lengthColumn.setCellValueFactory(new PropertyValueFactory<>("length"));
        getColumns().setAll(nameColumn, artistColumn, lengthColumn);

//        双击行
        setRowFactory(songTableView -> {
            TableRow<Song> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && (!row.isEmpty())) {
                    //todo 双击
                    Song emailInfo = row.getItem();
                    System.out.println(emailInfo);
//                    Main.app.play("file:///D:/CloudMusic/wxsn.mp3");




//                    Media media =new Media("http://music.163.com/song?id=31654408&userid=420718301");
                    System.out.println(new File("D:/CloudMusic/wxsn.mp3").toURI().toString());
                    Media media = new Media(new File("D:/CloudMusic/wxsn.mp3").toURI().toString());
//使用MediaPlayer 来播放音乐
                    MediaPlayer mediaPlayer=new MediaPlayer(media);
//                    mediaPlayer.setVolume(100);
////播放
//                    mediaPlayer.play();
                    System.out.println(mediaPlayer.getStatus());
                }
            });
            return row;
        });
//        单击列
        artistColumn.setCellFactory(songStringTableColumn -> {
            TableCell<Song, String> r = new CheckBoxTableCell<>(){
                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    this.setText(null);
                    this.setGraphic(null);
                    if (!empty) {
                        setText(item);
                    }
                }

            };
            r.setOnMouseClicked(mouseEvent -> {
//                todo 单击事件
                System.out.println(r.getTableRow().getItem());
            });
            return r;
        });
    }

    public void setitem(List<Song> songList) {
        ObservableList<Song> songs = FXCollections.observableList(songList);
        setItems(songs);
    }
}
